#

| Chapter 44 |

# 44. Shell Programming for the Uninitiated

Contents:
Everyone Should Learn Some Shell Programming
Writing a Simple Shell Program
What's a Shell, Anyway?
Testing How Your System Executes Files
Test String Values with Bourne Shell case
Pattern Matching in case Statements
Exit Status of UNIX Processes
Test Exit Status with the if Statement
Testing Your Success
Loops That Test Exit Status
Set Exit Status of a Shell (Script)
Trapping Exits Caused by Interrupts
read: Reading from the Keyboard
Putting awk, sed, etc., Inside Shell Scripts
Handling Command-Line Arguments in Shell Scripts
Handling Command-Line Arguments with a for Loop
Handling Arguments with while and shift
Standard Command-Line Parsing
The Bourne Shell set Command
test: Testing Files and Strings
Picking a Name for a New Command
Finding a Program Name; Multiple Program Names
Reading Files with the . and source Commands

## 44.1 Everyone Should Learn Some Shell Programming

One of the great things about UNIX is that it's made up of individual utilities, "building blocks" like cat and grep, that you run from a shell prompt. Using pipes, redirection, filters, and so on, you can combine those utilities to do an incredible number of things. Shell programming lets you take the same commands you'd type at a shell prompt - and put them into a file you can run by just typing its name. You can make new programs that combine UNIX programs (and other shell scripts) in your own way to do exactly what you need. If you don't like the way a program works, you can write a shell script to do just what you want.

Because many UNIX users use the shell every day, they don't need to learn a whole new language for programming... just some tips and techniques. In fact, this chapter covers a lot of programming techniques that you'll want to use even when you aren't programming. For example, loops and tests are handy on the command line.

(This series of articles does assume that you've written programs in some language before, or are generally familiar with programming concepts. If you haven't, you might start with a more comprehensive shell programming book.)

Some of the topics you need to learn about as a beginning shell programmer have already been covered in other chapters. Here are the articles you'll probably want to read - in an order that makes sense if you're looking for something of a tutorial:

- To see how to write a simple shell program, article 44.2. To embed scripts from other languages like sed and awk in a shell script, article 44.14.

- For explanation of shells in general, article 44.3.

- To test how your system executes files so you'll know how to write your shell programs, article 44.4.

- To read about environment and shell variables, articles 6.1 and 6.8, respectively.

- The echo command is covered by article 8.6.

- Shell quoting is explained in article 8.14.

- Test strings with a case statement, article 44.5. Match patterns in a case statement, article 44.6.

- Use the output of one command as arguments to another command with command substitution, article 9.16.

- Find out whether a program worked or failed with its exit status, article 44.7.

- Test a program's exit status and do different things if it worked or failed, articles 44.8 and 44.9.

- Loop through a set of commands and use another command to control that loop, article 44.10.

- Set exit status of a shell (shell script), article 44.11.

- Handle interrupts (like CTRL-c) and other signals, article 44.12.

- Read input from the keyboard, article 44.13.

- Handle command-line arguments (options, filenames, etc.), article 44.15.

- Step through arguments, or any list of words, with a for loop, articles 9.12 and 44.16.

- Handle arguments with the while and shift commands, article 44.17.

- Handle command-line arguments in a more standard and portable way with getopt, article 44.18.

- Set shell options and command-line arguments with the set command, article 44.19.

- Test files and strings of characters with the test command, article 44.20.

- Pick a name for a new command with no conflict, article 44.21.

- Find the name of a program and use it in the script, article 44.22.

- Use "subprograms" that can change the current environment, article 44.23.

This chapter discusses only Bourne shell programming. In most cases, the C shell isn't great for shell programming (47.2).

A note about command versions: unfortunately, the same commands on different versions of UNIX can have different options. Some Bourne shells are a little different from others. For instance, some test (44.20) commands have a -x option to test for an executable file; others don't. As article 46.10 explains, some echo commands use a -n option to mean "no newline at the end of this string"; others have you put `\c` at the end of the string. And so on. Where there are differences, these articles generally use the commands in Berkeley UNIX. If a command doesn't seem to work on your system, check its online manual page or the sh manual page.

- JP

VIII. Shell Programming |  | 44.2 Writing a Simple Shell Program